let maxBlockNum = 4
let pageNumArray = [1,8,1,7,8,2,7,2,1,8,3,8,2,1,3,1,7,1,3,7]
function LRU(maxBlockNum, pageNumArray){
    let blockArray = new Array(maxBlockNum)
    console.log(blockArray)
    let back = 0
    let count = 0
    for(let pageNum of pageNumArray){
        let used = blockArray.findIndex(function(value, index, arr) {
            return value == pageNum;
        })
        if(used == -1){ //页面不在内存块中
            if(back < maxBlockNum){
                blockArray[back] = pageNum
                back++
                console.log("缺页：页面" + pageNum + "加入内存块")
            }else{ //发生页面置换
                console.log("缺页并置换：访问页面" + pageNum + ", 页面" + blockArray[0] +"被置换出去")
                blockArray.splice(0,1)
                blockArray[maxBlockNum-1] = pageNum
            }
            count ++
        }else{ //页面在内存块中
            blockArray.splice(used,1)
            blockArray[back-1] = pageNum
            console.log("访问页面" + pageNum)
        }
    }
    console.log("总计缺页次数：" + count + "次")

}
LRU(maxBlockNum, pageNumArray)
